package com.ks.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ConvertVideo {

    private static String inputPath = "";

    private static String outputPath = "";

    private static String ffmpegPath = "";

    public static void main(String args[]) throws IOException {

        getPath();

        if (!checkfile(inputPath)) {
            System.out.println(inputPath + " is not file");
            return;
        }
        if (process()) {
            System.out.println("ok");
        }
    }

    private static void getPath() {
        // 先获取当前项目路径，在获得源文件、目标文件�?转换器的路径
        File diretory = new File("");
        try {
            String currPath = diretory.getAbsolutePath();
            inputPath = "C:\\apache-tomcat-8.5.34\\webapps\\sfeamapp\\WEB-INF\\upload\\qwe.mp4";
            outputPath = "C:\\apache-tomcat-8.5.34\\webapps\\sfeamapp\\WEB-INF\\upload\\MP4\\";
            ffmpegPath = "C:\\apache-tomcat-8.5.34\\webapps\\sfeamapp\\WEB-INF\\upload\\ffmpeg\\";
            System.out.println(currPath);
        } catch (Exception e) {
            System.out.println("getPath出错");
        }
    }

    private static boolean process() {

        System.out.println("直接转成mp4格式");
        return processMp4(inputPath);
    }


    private static boolean checkfile(String path) {
        File file = new File(path);

        return file.isFile();

    }

    /**
     * 对ffmpeg无法解析的文件格�?wmv9，rm，rmvb�?, 可以先用别的工具（mencoder）转换为avi(ffmpeg能解析的)格式.
     *
     * @param type
     * @return
     */
    private static String processAVI(int type) {
        List<String> commend = new ArrayList<>();
        commend.add(ffmpegPath + "mencoder");
        commend.add(inputPath);
        commend.add("-oac");
        commend.add("lavc");
        commend.add("-lavcopts");
        commend.add("acodec=mp3:abitrate=64");
        commend.add("-ovc");
        commend.add("xvid");
        commend.add("-xvidencopts");
        commend.add("bitrate=600");
        commend.add("-of");
        commend.add("mp4");
        commend.add("-o");
        commend.add(outputPath + "a.AVI");
        try {
            ProcessBuilder builder = new ProcessBuilder();
            Process process = builder.command(commend)
                    .redirectErrorStream(true).start();
            new PrintStream(process.getInputStream());
            new PrintStream(process.getErrorStream());
            process.waitFor();
            return outputPath + "a.AVI";
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * @param oldfilepath
     * @return
     */
    private static boolean processMp4(String oldfilepath) {

        if (!checkfile(inputPath)) {
            System.out.println(oldfilepath + " is not file");
            return false;
        }
        List<String> command = new ArrayList<>();
        command.add(ffmpegPath + "ffmpeg");
        command.add("-i");
        command.add(oldfilepath);
        command.add("-c:v");
        command.add("libx264");
        command.add("-mbd");
        command.add("0");
        command.add("-c:a");
        command.add("aac");
        command.add("-strict");
        command.add("-2");
        command.add("-pix_fmt");
        command.add("yuv420p");
        command.add("-movflags");
        command.add("faststart");
        command.add(outputPath + "a.mp4");
        try {

            Process videoProcess = new ProcessBuilder(command)
                    .redirectErrorStream(true).start();

            new PrintStream(videoProcess.getErrorStream()).start();

            new PrintStream(videoProcess.getInputStream()).start();

            videoProcess.waitFor();

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

class PrintStream extends Thread {
    private java.io.InputStream __is = null;

    PrintStream(java.io.InputStream is) {
        __is = is;
    }

    public void run() {
        try {
            int _ch = __is.read();
            if (_ch != -1)
                System.out.print((char) _ch);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}